﻿using MySql.Data.MySqlClient;
using NB.MGJ.FrameWork;
using NB.MGJ.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NB.MGJ.Service.Origins
{
    public partial class OriginsModuleService
    {
        private BaseRepository _baseRepository = new BaseRepository();

        #region  增
        public int Add(Origins_Module model)
        {
            int result = _baseRepository.InsertReturnID<Origins_Module>(model);
            return result;
        }
        #endregion

        #region 删
        public int Del(int id)
        {

            var sql = string.Format(" UPDATE Origins_Module  SET ViewDel=1 WHERE ModuleId = {0}", id);
            int result = _baseRepository.ExecuteSql<int>(sql);
            return result;
        }
        #endregion

        #region 改
        public int Update(Origins_Module model)
        {
            int result = _baseRepository.Update<Origins_Module>(model);
            return result;
        }
        #endregion

        #region 查
        public Origins_Module GetModel(int id)
        {
            string sql = "select * from Origins_Module where ModuleId=" +id;
            var model = _baseRepository.ExecuteSqlCollection<Origins_Module>(sql).ToList().FirstOrDefault();
            return model;
        }



        public PagedList<Origins_Module> GetPageList(ref PagerModel pager)
        {

            try
            {
                StringBuilder strSql1 = new StringBuilder();
                strSql1.Append(@" Origins_Module ");
                var strSql = new StringBuilder(strSql1.ToString().Trim(','));
                pager.FldName = " * ";
                pager.TblName = strSql.ToString();

                if (string.IsNullOrEmpty(pager.StrCondition) || pager.StrCondition == " ")
                {
                    pager.StrCondition = " ViewDel=0 ";
                }
                if (string.IsNullOrEmpty(pager.FldSort) || pager.FldSort == "")
                    pager.FldSort = "  ChangeDate desc ";

                MySqlParameter[] parameters = _baseRepository.SqParameterRefactor(ref pager);


                IEnumerable<Origins_Module> resultList = _baseRepository.ExecuteStoredProcedureCollection<Origins_Module>("Common_PageList", parameters);
                return new PagedList<Origins_Module>(resultList.ToList(), pager.Page, pager.PageSize, 1, Convert.ToInt32(parameters[6].Value));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }






        #endregion

        #region  其他
        public List<Origins_Module> GetModuleAllByMaterialId(int? id)
        {
            string sql = "select * from Origins_Module where MaterialId=" + id + " and ViewDel=0 order by Sort";
            var result = _baseRepository.ExecuteSqlCollection<Origins_Module>(sql).ToList();
            return result;
        }
        #endregion



        public int SortModule(List<int> list)
        {
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < list.Count; i++)
            {
                sb.Append("update Origins_Module set sort=" + i + " where ModuleId=" + list[i] + ";");
            }
            return _baseRepository.ExecuteSql<int>(sb.ToString().Trim());

        }

        
        

















    }
}
